﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Main.Model.DATA;

namespace Main.Model.Entity
{
    public class SinhVien
    {
        #region Constructors

        public SinhVien()
        { }


        public SinhVien(
            //duyệt để lấy danh sách các khóa để truy vấn lấy 1 đối tượng cho contructer 1 có tham số khởi tạo
            Guid
            sinhVienGuid
              )
        {
            //gọi hàm lấy thông tin Private với tham số là danh sách các khóa như trên 
            GetSinhVien(
                sinhVienGuid);
        }

        #endregion

        #region Private Properties

        private Guid sinhVienGuid = Guid.Empty;
        private string mSSV = string.Empty;
        private string hoTen = string.Empty;
        private DateTime ngaySinh;
        private Guid maTo = Guid.Empty;
        private int capDo = -1;

        #endregion

        #region Public Properties

        public Guid SinhVienGuid
        {
            get { return sinhVienGuid; }
            set { sinhVienGuid = value; }
        }
        public string MSSV
        {
            get { return mSSV; }
            set { mSSV = value; }
        }
        public string HoTen
        {
            get { return hoTen; }
            set { hoTen = value; }
        }
        public DateTime NgaySinh
        {
            get { return ngaySinh; }
            set { ngaySinh = value; }
        }
        public Guid MaTo
        {
            get { return maTo; }
            set { maTo = value; }
        }
        public int CapDo
        {
            get { return capDo; }
            set { capDo = value; }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of SinhVien.
        /// </summary>
        /// <param name="sinhVienGuid"> sinhVienGuid </param>
        private void GetSinhVien(
            Guid sinhVienGuid)
        {
            using (IDataReader reader = DBSinhVien.GetOne(
                sinhVienGuid))
            {
                PopulateFromReader(reader);
            }

        }


        private void PopulateFromReader(IDataReader reader)
        {
            if (reader.Read())
            {
                this.sinhVienGuid = new Guid(reader["SinhVienGuid"].ToString());
                this.mSSV = reader["MSSV"].ToString();
                this.hoTen = reader["HoTen"].ToString();
                this.ngaySinh = Convert.ToDateTime(reader["NgaySinh"]);
                this.maTo = new Guid(reader["MaTo"].ToString());
                this.capDo = Convert.ToInt32(reader["CapDo"]);

            }

        }
        public int GetCapDo(string username)
        {
            using (IDataReader reader = DBSinhVien.GetCapDo(username))
            {
                return PopulateGetCapDo(reader);
            }
        }

        private int PopulateGetCapDo(IDataReader reader)
        {
            if (reader.Read())
            {
                this.capDo = Convert.ToInt32(reader["CapDo"]);
            }
            return this.capDo;
        }
        /// <summary>
        /// Persists a new instance of SinhVien. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create()
        {
            this.sinhVienGuid = Guid.NewGuid();

            int rowsAffected = DBSinhVien.Create(
                this.sinhVienGuid,
                this.mSSV,
                this.hoTen,
                this.ngaySinh,
                this.maTo,
                this.capDo);

            return (rowsAffected > 0);

        }


        /// <summary>
        /// Updates this instance of SinhVien. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update()
        {

            return DBSinhVien.Update(
                this.sinhVienGuid,
                this.mSSV,
                this.hoTen,
                this.ngaySinh,
                this.maTo,
                this.capDo);

        }





        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance of SinhVien. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save()
        {
            if (this.sinhVienGuid != Guid.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
        }




        #endregion

        #region Static Methods

        /// <summary>
        /// Deletes an instance of SinhVien. Returns true on success.
        /// </summary>
        /// <param name="sinhVienGuid"> sinhVienGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(
                Guid sinhVienGuid)
        {
            return DBSinhVien.Delete(
                sinhVienGuid);
        }


        /// <summary>
        /// Gets a count of SinhVien. 
        /// </summary>
        public static int GetCount()
        {
            return DBSinhVien.GetCount();
        }

        public bool Login(string username, string password)
        {
            if (DBSinhVien.GetLogin(username, password) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private static List<SinhVien> LoadListFromReader(IDataReader reader)
        {
            List<SinhVien> sinhVienList = new List<SinhVien>();
            try
            {
                while (reader.Read())
                {
                    SinhVien sinhVien = new SinhVien();
                    sinhVien.sinhVienGuid = new Guid(reader["SinhVienGuid"].ToString());
                    sinhVien.mSSV = reader["MSSV"].ToString();
                    sinhVien.hoTen = reader["HoTen"].ToString();
                    sinhVien.ngaySinh = Convert.ToDateTime(reader["NgaySinh"]);
                    sinhVien.maTo = new Guid(reader["MaTo"].ToString());
                    sinhVien.capDo = Convert.ToInt32(reader["CapDo"]);
                    sinhVienList.Add(sinhVien);

                }
            }
            finally
            {
                reader.Close();
            }

            return sinhVienList;

        }

        /// <summary>
        /// Gets an IList with all instances of SinhVien.
        /// </summary>
        public static List<SinhVien> GetAll()
        {
            IDataReader reader = DBSinhVien.GetAll();
            return LoadListFromReader(reader);

        }

        /// <summary>
        /// Gets an IList with page of instances of SinhVien.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static List<SinhVien> GetPage(int pageNumber, int pageSize, out int totalPages)
        {
            totalPages = 1;
            IDataReader reader = DBSinhVien.GetPage(pageNumber, pageSize, out totalPages);
            return LoadListFromReader(reader);
        }



        #endregion

    }

}
